package one.stand.controller;

import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import one.stand.aop.ApiController;
import one.stand.model.ResultModel;
import one.stand.request.*;
import one.stand.response.OrdersResponse;
import org.springframework.beans.BeanUtils;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.io.BufferedReader;
import java.util.List;

/**
 * 订单信息
 * @author
 */
@RestController
@RequestMapping("crmApi/orders")
public class OrdersController extends BaseController {

    @PostMapping("/selectOrdersInfo")
    @ApiOperation(value = "根据公司查询订单(订单状态（1待付款，2待发货，3待收货，4已完成，5交易取消，6退款成功）)")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "orderState", value = "订单状态（1待付款，2待发货，3待收货，4已完成，5交易取消，6退款成功）", required = true, paramType = "Integer"),
    })
    @ApiController(isLogin = true,company = true,card = true,paramClass =OrdersRequest.class )
    public ResultModel selectOrdersInfo(@RequestAttribute OrdersRequest request) {
        
        return crmFegin.selectOrdersInfo(request);
    }

    @PostMapping("/getOrdersInfo")
    @ApiOperation(value = "查询订单详情")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "ordersId", value = "订单id", required = true, paramType = "Integer"),
    })
    @ApiController(isLogin = true,company = true,card = true,paramClass =OrdersRequest.class)
    public ResultModel<List<OrdersResponse>> getOrdersInfo(@RequestAttribute OrdersRequest request) {
        
        return crmFegin.getOrdersInfo(request);
    }

    @PostMapping("/updateOrderState")
    @ApiOperation(value = "撤销订单，确认发货")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "ordersId", value = "订单id", required = true, paramType = "Integer"),
            @ApiImplicitParam(name = "orderState", value = "订单状态（1待付款，2待发货，3待收货，4已完成，5交易取消，6退款成功）", required = true, paramType = "Integer"),
    })
    @ApiController(isLogin = true,company = true,card = true,paramClass =OrdersRequest.class)
    public ResultModel updateOrderState(@RequestAttribute OrdersRequest request) {
        
        return crmFegin.updateOrderState(request);
    }

    @PostMapping("/updateRefundState")
    @ApiOperation(value = "修改订单退款状态1未申请，2退款申请中，3退款成功")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "ordersId", value = "订单id", required = true, paramType = "Integer"),
            @ApiImplicitParam(name = "refundState", value = "退款状态1未申请(拒绝)，2退款申请中，3退款成功", required = true, paramType = "Integer"),
    })
    @ApiController(isLogin = true,company = true,card = true,paramClass =OrdersRequest.class)
    public ResultModel updateRefundState(@RequestAttribute OrdersRequest request) {
        
        return crmFegin.updateRefundState(request);
    }

    @RequestMapping(value = "/backRefund")
    @ApiOperation(value = "退款异步回调")
    @ApiController(isParam = false)
    public String backRefund(HttpServletRequest request) {
        try {
            BufferedReader reader = request.getReader();
            String line = "";
            StringBuffer inputString = new StringBuffer();
            while ((line = reader.readLine()) != null) {
                inputString.append(line);
            }
            request.getReader().close();
            return crmFegin.backRefund(inputString.toString());
        }catch (Exception e){
            e.printStackTrace();
        }
        return fail();
    }

    public String fail() {
        return "<xml> \n" +
                "  <return_code><![CDATA[fail]]></return_code>\n" +
                "  <return_msg><![CDATA[OK]]></return_msg>\n" +
                "</xml>";
    }
}
